package com.ollobot.authorization.controller;

import com.ollobot.authorization.entity.User;
import com.ollobot.authorization.security.CustomOAuth2User;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/user")
@RequiredArgsConstructor
public class UserController {

    @GetMapping("/info")
    public ResponseEntity<?> getUserInfo(Authentication authentication) {
        if (authentication == null || !authentication.isAuthenticated()) {
            return ResponseEntity.status(401).body(Map.of("error", "User not authenticated"));
        }

        Object principal = authentication.getPrincipal();
        Map<String, Object> userInfo = new HashMap<>();

        if (principal instanceof User) {
            User user = (User) principal;
            userInfo.put("id", user.getId());
            userInfo.put("username", user.getUsername());
            userInfo.put("email", user.getEmail());
            userInfo.put("firstName", user.getFirstName());
            userInfo.put("lastName", user.getLastName());
            userInfo.put("avatarUrl", user.getAvatarUrl());
            userInfo.put("provider", user.getProvider());
            userInfo.put("emailVerified", user.getEmailVerified());
            userInfo.put("enabled", user.isEnabled());
        } else if (principal instanceof CustomOAuth2User) {
            CustomOAuth2User oauth2User = (CustomOAuth2User) principal;
            User user = oauth2User.getUser();
            userInfo.put("id", user.getId());
            userInfo.put("username", user.getUsername());
            userInfo.put("email", user.getEmail());
            userInfo.put("firstName", user.getFirstName());
            userInfo.put("lastName", user.getLastName());
            userInfo.put("avatarUrl", user.getAvatarUrl());
            userInfo.put("provider", user.getProvider());
            userInfo.put("emailVerified", user.getEmailVerified());
            userInfo.put("enabled", user.isEnabled());
        } else {
            userInfo.put("username", authentication.getName());
            userInfo.put("authorities", authentication.getAuthorities());
        }

        return ResponseEntity.ok(userInfo);
    }

    @GetMapping("/profile")
    public ResponseEntity<?> getUserProfile(@AuthenticationPrincipal Object principal) {
        if (principal == null) {
            return ResponseEntity.status(401).body(Map.of("error", "User not authenticated"));
        }

        Map<String, Object> profile = new HashMap<>();

        if (principal instanceof User) {
            User user = (User) principal;
            profile.put("user", user);
        } else if (principal instanceof CustomOAuth2User) {
            CustomOAuth2User oauth2User = (CustomOAuth2User) principal;
            profile.put("user", oauth2User.getUser());
            profile.put("attributes", oauth2User.getAttributes());
        }

        return ResponseEntity.ok(profile);
    }
}
